package com.wish.g1.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.business.entity.Tenant;
import com.wish.g1.business.service.ITenantService;
import com.wish.g1.common.core.utils.StringUtils;
import com.wish.g1.common.core.utils.poi.ExcelUtil;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.web.page.TableDataInfo;
import com.wish.g1.common.log.annotation.Log;
import com.wish.g1.common.log.enums.BusinessType;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.system.api.RemoteRegionService;
import com.wish.g1.system.api.domain.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户表 前端控制器
 * </p>
 *
 * @author Dragon
 * @since 2022-11-22
 */
@RestController
@RequestMapping("/tenant")
public class TenantControllerextends extends BaseController
{
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private RemoteRegionService remoteRegionService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 查询商户列表
     */
    // @RequiresPermissions("business:tenant:list")
    @GetMapping("/list")
    public TableDataInfo list(Tenant tenant) {
        startPage();
        QueryWrapper<Tenant> wrapper = new QueryWrapper<>();
        // 商户类型
        if (tenant.getMerchantName()!=null){
            wrapper.eq("merchant_name",tenant.getMerchantName());
        }
        // 商户名称
        if (StringUtils.hasText(tenant.getTenantName())){
            wrapper.like("tenant_name",tenant.getTenantName());
        }
        List<Tenant> list = tenantService.list(wrapper);
        List<Region> regions = remoteRegionService.getRegionList();
        Map<Integer, String> collect = regions.stream().collect(Collectors.toMap(Region::getId, Region::getName));
        for (Tenant tenant1 : list) {
            tenant1.setRegionName1(collect.get(Integer.parseInt(tenant1.getRegionId1())));
            tenant1.setRegionName2(collect.get(Integer.parseInt(tenant1.getRegionId2())));
            tenant1.setRegionName3(collect.get(Integer.parseInt(tenant1.getRegionId3())));
        }
        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @RequiresPermissions("business:tenant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tenant tenant)
    {
        List<Tenant> list = tenantService.list(null);
        ExcelUtil<Tenant> util = new ExcelUtil<Tenant>(Tenant.class);
        util.exportExcel(response, list, "商户数据");
    }

    /**
     * 获取商户详细信息
     */
    @RequiresPermissions("business:tenant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tenantService.getById(id));
    }

    /**
     * 新增商户
     */
    // @GlobalTransactional
    // @RequiresPermissions("business:tenant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Tenant tenant)
    {
        tenant.setCreateTime(LocalDateTime.now());
        boolean b = tenantService.saveOrUpdate(tenant);
        // int i = 1/0;
        threadPoolTaskExecutor.execute(()->{
            kafkaTemplate.send("business", UUID.randomUUID().toString(), "成功入驻");
        });
        return toAjax(b);
    }

    /**
     * 修改商户
     */
    @RequiresPermissions("business:tenant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tenant tenant)
    {
        return toAjax(tenantService.saveOrUpdate(tenant));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("business:tenant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            tenantService.removeById(id);
        }
        return toAjax(1);
    }
}
